package org.vnl.platform.web.impl.myfaces.dojo.component.renderer;

import java.io.IOException;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.vnl.platform.web.impl.myfaces.dojo.component.DojoTextBox;


abstract public class AbstractDojoTextBoxRenderer extends DojoRendererBase {
    
    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        if (!(component instanceof DojoTextBox)) {
            return;
        }
        DojoTextBox box = (DojoTextBox) component;
        String clientId = box.getClientId(context);
        ResponseWriter rw = context.getResponseWriter();
        rw.startElement("input", box);
        writeGeneralProperty(rw, "type", "text");
        writeGeneralProperty(rw, "value", box.getValue());
        writeDojoId(rw, box.getDojoId(), clientId);
        writeDojoType(rw, getDojoType());
        writeDojoProps(rw, box.getDojoProps());
        writeGeneralProperty(rw, "name", clientId);
        writeGeneralProperty(rw, "id", clientId);
    }

    abstract protected String getDojoType();
    abstract void afterTagStart(ResponseWriter rw, DojoTextBox box) throws IOException;
    abstract void afterTagEnd(ResponseWriter rw, DojoTextBox box) throws IOException;
    abstract void beforeTagStart(ResponseWriter rw, DojoTextBox box) throws IOException;

    @Override
    public void decode(FacesContext context, UIComponent component) {
        if (!component.isRendered()) {
            return;
        }
        if (!(component instanceof DojoTextBox)) {
            return;
        }
        Map<String, String> params = context.getExternalContext().getRequestParameterMap();
        DojoTextBox box = (DojoTextBox) component;
        String value = params.get(box.getClientId(context));
        if (value != null) {
            box.setSubmittedValue(value);
        }
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
        ResponseWriter rw = context.getResponseWriter();
        rw.endElement("input");
    }
    
    
}
